<template>
	<yb-waterfall
	custom-class="yb-flex-1"
	ref="list"
	type="page"
	:leftGap="15"
	:rightGap="15"
	:columnGap="15"
	:bounce="bounce"
	:empty="emptySync"
	:loading="loadingSync"
	:pulldown="pulldownSync"
	:loadmore="loadmoreSync"
	:enableScrolling="enableScrolling"
	@scroll="onScroll"
	@pulldown="onPulldown"
	@loadmore="onLoadmore">
		<slot></slot>
		<yb-waterfall-item v-for="(item, index) in listSync" :key="item.id + '_' + index + '_' + item.type">
			<view class="yb-flex waterfall-item" :style="{
				'background-color': skinColor.color_bg_1
			}" @tap="onItemClick(item)" @longpress="onShowItemMore(item)">
				<view class="cover-box">
					<rd-image lazyLoad min-height="400" borderTopLeftRadius="20" borderTopRightRadius="20" waterfall :src="item.cover" mode="widthFix"></rd-image>
					<view class="cover-mask"></view>
				</view>
				<view class="waterfall-info yb-flex">
					<yb-text v-if="item.title" custom-style="margin-top: 10rpx;" :lines="2" :size="26" :color="skinColor.color_1" :value="item.title"></yb-text>
					<yb-text custom-style="margin-top: 10rpx" :lines="4" :size="22" :color="skinColor.color_3" :value="item.desc || '这是一部来自《' + app.$business.filterSource(item.source).title + '》的' + app.$business.getType(item.type)"></yb-text>
					<view class="yb-flex yb-row yb-wrap" style="margin-top: 10rpx;" v-if="item.tag">
						<yb-text size="22" custom-style="margin-right:10rpx;margin-bottom:10rpx" :color="skinColor.color_theme_1" v-for="(tag, i) in item.tag.split(',')" :value="'#'+tag" :key="i"></yb-text>
					</view>
					<yb-text v-if="item.latest" :size="22" :lineHeight="26" :lines="1" :color="skinColor.color_orange" custom-style="height: 26rpx;margin-top: 5rpx" :value="'更新至：' + item.latest"></yb-text>
					<view class="yb-flex yb-row yb-align-center" style="margin-top: 20rpx;">
						<rd-icon class="icon" name="person" size="25" :color="skinColor.color_2"></rd-icon>
						<yb-text custom-class="yb-flex-1" custom-style="margin-left: 10rpx" class="name" :size="24" :lines="1" :color="skinColor.color_1" :value="item.creator || app.$business.filterSource(item.source).title" />
						<yb-tag custom-style="margin-left: 10rpx" :options="{
							size: 18,
							color: skinColor.color_white,
							bgColor: skinColor.color_orange
						}" :value="app.$business.getType(item.type)"></yb-tag>
					</view>
				</view>
			</view>
		</yb-waterfall-item>
	</yb-waterfall>
</template>

<script>
	import appMixin from '@/common/mixin/app.js';
	export default {
		mixins: [appMixin],
		props: {
			list: {
				type: Array,
				default () {
					return new Array
				}
			},
			index: {
				type: [Number, String],
				default: -1
			},
			enableScrolling: {
				type: Boolean,
				default: false
			},
			bounce: {
				type: Boolean,
				default: true
			},
			loading: {
				type: [Boolean,Object,String],
				default: false
			},
			empty: {
				type: [Boolean,Object,String],
				default: false
			},
			pulldown: {
				type: [Boolean,Object],
				default: false
			},
			loadmore: {
				type: [Boolean,Object],
				default: false
			},
			//显示排名
			rank: {
				type: Boolean,
				default: false
			},
			//开启自定义点击事件
			customclickenable: {
				type: Boolean,
				default: false
			}
		},
		computed: {
			emptySync () {
				return Object.assign({}, {
					show: false,
					text: '暂无数据',
					color: this.skinColor.color_gap_1,
					bgColor: this.skinColor.color_bg_1
				}, this.app.$utils.typeof(this.empty) == 'String' ? {text: this.empty} : this.app.$utils.typeof(this.empty) == 'Boolean' ? {show: this.empty} : this.empty)
			},
			loadingSync () {
				return Object.assign({}, {
					show: false,
					text: '加载中',
					color: this.skinColor.color_1,
					bgColor: this.skinColor.color_bg_1
				}, this.app.$utils.typeof(this.loading) == 'String' ? {text: this.loading} : this.app.$utils.typeof(this.loading) == 'Boolean' ? {show: this.loading} : this.loading)
			},
			pulldownSync () {
				return Object.assign({}, {
					show: false,
					color: this.skinColor.color_1
				}, this.app.$utils.typeof(this.pulldown) == 'Boolean' ? {show: this.pulldown} : this.pulldown)
			},
			loadmoreSync () {
				return Object.assign({}, {
					show: !this.isLastPage,
					color: this.skinColor.color_1
				}, this.app.$utils.typeof(this.loadmore) == 'Boolean' ? {show: this.loadmore} : this.loadmore)
			},
			listSync () {
				return this.list.slice(0, Math.min(this.page * this.size, this.list.length))
			},
			isLastPage () {
				return this.page * this.size >= this.list.length
			}
		},
		data () {
			return {
				page: 1,
				size: 20
			}
		},
		methods: {
			onPulldown (callback) {
				this.page = 1
				if ( this.index > -1 ) {
					this.$emit('pulldown', this.index, callback)
				} else {
					this.$emit('pulldown', callback)
				}
			},
			onLoadmore (callback) {
				if ( this.isLastPage ) {
					if ( this.page * this.size == this.listSync.length ) {
						this.page++
					} 
					if ( this.index > -1 ) {
						this.$emit('loadmore', this.index, callback)
					} else {
						this.$emit('loadmore', callback)
					}
				} else {
					this.page++
					callback('success')
				}
			},
			onItemClick (params) {
				if ( this.customclickenable ) {
					//自定义点击事件
					this.$emit('itemClick', params)
				} else {
					this.app.$business.openDetail(params)
				}
			},
			onScroll (e) {
				this.$emit('scroll', e)
			},
			onShowItemMore (params) {
				this.$emit('itemLongpress', params)
			},
			resetLoadmore () {
				this.$refs.list.resetLoadmore()
			},
			backTop () {
				this.$refs.list.backTop()
			},
			setLoadmoreEnd () {
				if ( this.isLastPage ) this.$refs.list.setLoadmoreEnd()
			},
			setLoadmoreSuccess() {
				if ( this.isLastPage ) this.$refs.list.setLoadmoreSuccess()
			},
			setLoadmoreFail() {
				if ( this.isLastPage ) this.$refs.list.setLoadmoreFail()
			}
		},
		watch: {
			isLastPage (newVal) {
				if ( !newVal ) {
					this.$refs.list && this.$refs.list.setLoadmoreSuccess()
				}
			},
			list (newVal) {
				if ( newVal.length < this.page * this.size ) {
					this.page = Math.ceil(newVal.length / 20) > 0 ? Math.ceil(newVal.length / 20) : 1
				}
			}
		},
	}
</script>

<style>
	.waterfall-item {
		border-radius: 20rpx;
	}
	.waterfall-info {
		padding: 10rpx;
	}
	.waterfall-item .cover-box {
		position: relative;
	}
	.waterfall-item .cover-mask {
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background-color: rgba(0,0,0,.2);
		border-top-left-radius: 20rpx;
		border-top-right-radius: 20rpx;
	}
</style>